package com.jiefeng.manager.service.dataexcel.impl;

import com.jiefeng.manager.common.util.ExcelBatchExport;
import com.jiefeng.manager.common.util.ExcelBatchImport;
import com.jiefeng.manager.service.dataexcel.DataExcelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.File;
import java.io.OutputStream;
import java.util.List;

/**
 * 匹配数据Excel导入导出服务接口实现
 *
 * Created by WuZhiqi on 2016/5/26.
 */
@Service
public class ObjectDataExcelServiceImpl extends DataExcelService<Object> {

    private static final Logger logger = LoggerFactory.getLogger(ObjectDataExcelServiceImpl.class);

    public ObjectDataExcelServiceImpl() {
        super(Object.class);
    }

    @Override
    public void exportExcelTemplate(OutputStream outputStream, EXCEL_VERSION excelVersion) {
        try{
            if(excelVersion == EXCEL_VERSION.VERSION_XLS){
                ExcelBatchExport.xslExcelBatchExport(outputStream, Object.class, null);
            }else if(excelVersion == EXCEL_VERSION.VERSION_XLSX){
                ExcelBatchExport.xslxExcelBatchExport(outputStream, Object.class, null);
            }else{
                logger.error("please check excelVersion must be not null and valid!");
            }
        }catch (Exception e){
            logger.error("导出匹配数据Excel模板异常!", e);
        }
    }

    @Override
    public void exportExcel(List<Object> exportList, OutputStream outputStream, EXCEL_VERSION excelVersion) {
        try{
            if(excelVersion == EXCEL_VERSION.VERSION_XLS){
                ExcelBatchExport.xslExcelBatchExport(outputStream, Object.class, exportList);
            }else if(excelVersion == EXCEL_VERSION.VERSION_XLSX){
                ExcelBatchExport.xslxExcelBatchExport(outputStream, Object.class, exportList);
            }else{
                logger.error("please check excelVersion must be not null and valid!");
            }
        }catch (Exception e){
            logger.error("导出匹配数据Excel模板异常!", e);
        }
    }

    @Override
    public void importExcel(File file) {
        Assert.notNull(file, "param file must be not null");
        logger.info(String.format("导入的Excel文件大小，file.length() = %s", file.length()));
            try{
                List<Object> matchedDataPoList = ExcelBatchImport.excelBatchImport(file, Object.class);

            }catch (Exception exception){
                logger.error("Excel批量导入匹配数据异常!", exception);
            }
    }
}
